<?php

namespace app\models;

use app\common\utils\HttpClient;
use Yii;
use Overtrue\Wechat\QRCode;


/**
 * This is the model class for table "gky_we_account".
 *
 * @property integer $id
 * @property string $wename
 * @property string $weno
 * @property integer $wetype
 * @property string $weoid
 * @property string $weappid
 * @property string $wesecret
 * @property string $weadskey
 * @property string $qrcode
 * @property string $addtime
 * @property string $accesstoken
 * @property string $atexpiretime
 * @property string $wetoken
 * @property string $wemenu
 * @property string $jsapiticket
 * @property string $jsapiexpiretime
 */
class GkyWeAccount extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'gky_we_account';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['wetype', 'addtime', 'atexpiretime', 'jsapiexpiretime'], 'integer'],
            [['wemenu'], 'string'],
            [['wename', 'weno', 'weoid', 'weappid'], 'string', 'max' => 128],
            [['wesecret', 'weadskey', 'accesstoken', 'wetoken', 'jsapiticket'], 'string', 'max' => 256],
            [['qrcode'], 'string', 'max' => 512]
        ];
    }

    public function getToken(){

        if(!isset($this->jsapiexpiretime) || $this->jsapiexpiretime < time()){
            $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='
                .$this->weappid.'&secret='.$this->wesecret;
            $result = HttpClient::get($url);
            $this -> accesstoken = $result -> access_token;
            $this -> jsapiexpiretime = $result -> expires_in + time();
            $this->save();
            return $result -> access_token;
        }else{
            return $this->accesstoken;
        }
    }

    public function findQrcode(){

        if($this->qrcode == null){
            $qrcode = new QRCode($this->weappid, $this->wesecret);
            $result = $qrcode->forever(56);// 或者 $qrcode->forever("foo");
            $ticket = $result->ticket;
            $this->qrcode = $qrcode->show($ticket);
            $this->save();
        }

        return $this->qrcode;
    }

    public static function findQrcodeById($id){

        $weaccount = GkyWeAccount::find()->where(['id'=>$id])->one();
        if($weaccount->qrcode == null){
            $qrcode = new QRCode($weaccount->weappid, $weaccount->wesecret);
            $result = $qrcode->forever(56);// 或者 $qrcode->forever("foo");
            $ticket = $result->ticket;
            $weaccount->qrcode = $qrcode->show($ticket);
            $weaccount->save();
        }

        return $weaccount->qrcode;
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'wename' => '名称',
            'weno' => '微信号',
            'wetype' => '类型',
            'weoid' => '原始ID',
            'weappid' => '应用ID',
            'wesecret' => 'AppSecret',
            'weadskey' => 'EncodingAESKey',
            'qrcode' => '二维码',
            'addtime' => '添加时间',
            'accesstoken' => 'Accesstoken',
            'atexpiretime' => 'Atexpiretime',
            'wetoken' => 'Token',
            'wemenu' => 'Wemenu',
            'jsapiticket' => 'Jsapiticket',
            'jsapiexpiretime' => 'Jsapiexpiretime',
        ];
    }
}
